package servlet;

import entity.*;
import service.IOrderService;
import service.impl.OrderServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

@WebServlet(name = "cos", value = "/create_order")
public class CreateOrderServlet extends HttpServlet {
    private IOrderService order = new OrderServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("lu");
        Map<Product, Integer> shoppingcar = (Map<Product, Integer>) session.getAttribute("sc");
        Order order = this.order.create(loginUser, shoppingcar);
        Set<OrderItem> items = order.getItems();
        session.setAttribute("orderitem", items);
        for (OrderItem item : items) {
            String info = item.getProduct().getInfo();
            System.out.println(info);
        }
        response.sendRedirect("order.jsp");


    }

}
